<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_mutasi_mahasiswa extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'a.kd_prodi';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		//$kd_fakultas = isset($_POST['kd_fakultas']) ? strval($_POST['kd_fakultas']) : '';
		$kd_prodi = isset($_POST['kd_prodi']) ? strval($_POST['kd_prodi']) : '';
		$tahun = isset($_POST['tahun']) ? strval($_POST['tahun']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.kd_mahasiswa, a.thn_angkatan, a.no_pendaftaran, a.nama, a.tempat_lahir, a.tgl_lahir, a.alamat, a.no_telp, b.nama_prodi, a.nama_ortu, a.alamat_ortu, a.pekerjaan, a.telp_ortu, a.verifikasi, a.asal_universitas, a.telp_asal_universitas');
			$this->db->from('tbl_mutasi_mahasiswa a');
			$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
			//$this->db->join('ref_dosen c', 'c.kd_dosen = a.kd_dosen');
			
			if($kd_prodi != ''){
				$this->db->where('a.kd_prodi', $kd_prodi);
			}
			if($tahun != ''){
				$this->db->where('a.thn_angkatan', $tahun);
			}
			$this->db->where('a.verifikasi', 'Belum');
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	//get data master
	function getdata_master($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'a.kd_prodi';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		//$kd_fakultas = isset($_POST['kd_fakultas']) ? strval($_POST['kd_fakultas']) : '';
		$kd_prodi = isset($_POST['kd_prodi']) ? strval($_POST['kd_prodi']) : '';
		$tahun = isset($_POST['tahun']) ? strval($_POST['tahun']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.kd_mahasiswa, a.nim, a.thn_angkatan, a.no_pendaftaran, a.nama, a.tempat_lahir, a.tgl_lahir, a.alamat, a.no_telp, b.nama_prodi, a.nama_ortu, a.alamat_ortu, a.pekerjaan, a.telp_ortu, a.verifikasi, a.asal_universitas, a.telp_asal_universitas');
			$this->db->from('tbl_mutasi_mahasiswa a');
			$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
			//$this->db->join('ref_dosen c', 'c.kd_dosen = a.kd_dosen');
			
			if($kd_prodi != ''){
				$this->db->where('a.kd_prodi', $kd_prodi);
			}
			if($tahun != ''){
				$this->db->where('a.thn_angkatan', $tahun);
			}
			$this->db->where('a.verifikasi', 'Sudah');
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function togrid($data, $count=0){
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
			$response->total = $count;
		}else{
		$response->rows = array();
		}
		return json_encode($response);
	}
	
	function isExistKode($kode){
		if ($kode!=null)
			$this->db->where('kd_mahasiswa',$kode);
		
		$this->db->select('*');
		$this->db->from('tbl_mutasi_mahasiswa');
		$query = $this->db->get();
		
		$rs = $query->num_rows() ;		
		$query->free_result();
		
		return ($rs>0);
	}
	
	function getdataedit($kode){
		$this->db->flush_cache();
		$this->db->select('a.kd_mahasiswa, b.kd_fakultas, a.nim, a.thn_angkatan, a.no_pendaftaran, a.nama, a.tempat_lahir, a.tgl_lahir, a.alamat, a.no_telp, a.kd_prodi, a.nama_ortu, a.alamat_ortu, a.pekerjaan, a.telp_ortu, a.verifikasi, a.asal_universitas, a.telp_asal_universitas,a.lampiran');
		$this->db->from('tbl_mutasi_mahasiswa a');
		$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
		$this->db->join('ref_fakultas c', 'c.kd_fakultas = b.kd_fakultas','left');
		//$this->db->join('ref_dosen d', 'd.kd_dosen = a.kd_dosen','left');
		
		$this->db->where('a.kd_mahasiswa', $kode);
		return $this->db->get();
	}
	
	//detail
	function getdatadetail($kode){
		$this->db->flush_cache();
		$this->db->select('a.kd_mahasiswa, c.nama_fakultas, a.nim, a.thn_angkatan, a.no_pendaftaran, a.nama, a.alamat, a.no_telp, b.nama_prodi, a.nama_ortu, a.alamat_ortu, a.pekerjaan, a.telp_ortu, a.verifikasi, a.asal_universitas, a.telp_asal_universitas,a.lampiran');
		$this->db->from('tbl_mutasi_mahasiswa a');
		$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
		$this->db->join('ref_fakultas c', 'c.kd_fakultas = b.kd_fakultas','left');
		//$this->db->join('ref_dosen d', 'd.kd_dosen = a.kd_dosen','left');
		
		$this->db->where('a.kd_mahasiswa', $kode);
		return $this->db->get();
	}
	
	function InsertOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('thn_angkatan', $data['thn_angkatan']);
		$this->db->set('no_pendaftaran', $data['no_pendaftaran']);
		//$this->db->set('nim', $data['nim']);
		$this->db->set('nama', $data['nama']);
		$this->db->set('tempat_lahir', $data['tempat_lahir']);
		$this->db->set('tgl_lahir', FormatDateToMysql($data['tgl_lahir']));
		$this->db->set('alamat', $data['alamat']);
		$this->db->set('no_telp', $data['no_telp']);
		$this->db->set('kd_prodi', $data['kd_prodi']);
		//$this->db->set('kd_dosen', $data['kd_dosen']);
		$this->db->set('nama_ortu', $data['nama_ortu']);
		$this->db->set('alamat_ortu', $data['alamat_ortu']);
		$this->db->set('pekerjaan', $data['pekerjaan']);
		$this->db->set('telp_ortu', $data['telp_ortu']);
		$this->db->set('asal_universitas', $data['asal_universitas']);
		$this->db->set('telp_asal_universitas', $data['telp_asal_universitas']);
		if($data['file_upload'] != ''){
			$this->db->set('lampiran', $data['file_upload']);
		}
		//$this->db->set('verifikasi', $data['verifikasi']);
		$result = $this->db->insert('tbl_mutasi_mahasiswa');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function InsertOnDBverifikasi($data){
		$this->db->trans_start();
		
				# update table mutasi mahasiswa
				$this->db->flush_cache();
				$this->db->set('nim', $data['nim']);
				$this->db->set('verifikasi', 'Sudah');
				$this->db->where('kd_mahasiswa', $data['kd_mahasiswa']);
				$this->db->update('tbl_mutasi_mahasiswa');
				
				# insert to table mahasiswa
				$this->db->flush_cache();
				$this->db->set('thn_angkatan', $data['thn_angkatan']);
				$this->db->set('nim', $data['nim']);
				$this->db->set('nama', $data['nama']);
				$this->db->set('tempat_lahir', $data['tempat_lahir']);
				$this->db->set('tgl_lahir', FormatDateToMysql($data['tgl_lahir']));
				$this->db->set('alamat', $data['alamat']);
				$this->db->set('no_telp', $data['no_telp']);
				$this->db->set('kd_prodi', $data['kd_prodi']);
				$this->db->set('kd_dosen', $data['kd_dosen']);
				$this->db->set('nama_ortu', $data['nama_ortu']);
				$this->db->set('alamat_ortu', $data['alamat_ortu']);
				$this->db->set('pekerjaan', $data['pekerjaan']);
				$this->db->set('telp_ortu', $data['telp_ortu']);
				$this->db->set('status', 'Mahasiswa Aktif');
				
				$this->db->set('CUID', $this->session->userdata('user_id'));
				$this->db->set('CDATE', date('Y-m-d H:i:s'));
				
				$this->db->insert('tbl_mahasiswa');
		
		$this->db->trans_complete();
	    return $this->db->trans_status();
	}
	
	function get_pdf($fil){
		
		# get data
		$this->db->flush_cache();
		$this->db->start_cache();
		$this->db->select('a.nim, a.kd_mahasiswa, a.thn_angkatan, a.no_pendaftaran, a.nama, a.alamat, a.no_telp, b.nama_prodi, a.nama_ortu, a.alamat_ortu, a.pekerjaan, a.telp_ortu, a.verifikasi, a.asal_universitas, a.telp_asal_universitas, c.nama_fakultas');
		$this->db->from('tbl_mutasi_mahasiswa a');
		$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
		$this->db->join('ref_fakultas c', 'c.kd_fakultas = b.kd_fakultas');
					
			$this->db->where('a.kd_prodi', $fil['kd_prodi']);
			$this->db->where('a.thn_angkatan', $fil['thn_angkatan']);
			
			//$this->db->where('a.status', 'A');

		// proses
		$result = $this->db->get();
		
		if ($result->num_rows() > 0) {
            foreach ($result->result() as $data) {
                $data_pdf[] = $data;
            }
            return $data_pdf;           
        }
	}
	
}

?>